﻿namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class DmGhiChuCachDungDataAccess
    {
        public bool InsertDmGhiChuCachDung(DmGhiChuCachDungEntity obj)
        {
            try
            {
                string sql = "spDmGhiChuCachDung_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaGhiChuCachDung", DbType.String, obj.MaGhiChuCachDung);
                db.AddInParameter(cmd, "TenGhiChuCachDung", DbType.String, obj.TenGhiChuCachDung);
                db.AddInParameter(cmd, "MoTaGhiChu", DbType.String, obj.MoTaGhiChu);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);                
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertDmGhiChuCachDung", ex.Message); throw ex; }
        }

        public bool EditDmGhiChuCachDung(DmGhiChuCachDungEntity obj)
        {
            try
            {
                string sql = "spDmGhiChuCachDung_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaGhiChuCachDung", DbType.String, obj.MaGhiChuCachDung);
                db.AddInParameter(cmd, "TenGhiChuCachDung", DbType.String, obj.TenGhiChuCachDung);
                db.AddInParameter(cmd, "MoTaGhiChu", DbType.String, obj.MoTaGhiChu);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateDmGhiChuCachDung", ex.Message); throw ex; }
        }

        public bool DeleteDmGhiChuCachDung(string maGhiChuCachDung)
        {
            try
            {
                string sql = "spDmGhiChuCachDung_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaGhiChuCachDung", DbType.String, maGhiChuCachDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteDmGhiChuCachDung", ex.Message); throw ex; }
        }

        public DmGhiChuCachDungEntity GetDmGhiChuCachDung(string maGhiChuCachDung)
        {
            try
            {
                DmGhiChuCachDungEntity obj = null;
                string sql = "spDmGhiChuCachDung_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaGhiChuCachDung", DbType.String, maGhiChuCachDung);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmGhiChuCachDungEntity();
                        obj.MaGhiChuCachDung = reader["MaGhiChuCachDung"].ToString();
                        obj.TenGhiChuCachDung = reader["TenGhiChuCachDung"].ToString();
                        obj.MoTaGhiChu = reader["MoTaGhiChu"].ToString();
                        obj.TrangThaiSuDung = (bool)reader["TrangThaiSuDung"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmGhiChuCachDung", ex.Message); throw ex; }
        }

        public List<DmGhiChuCachDungEntity> GetDmGhiChuCachDungList()
        {
            try
            {
                List<DmGhiChuCachDungEntity> list = new List<DmGhiChuCachDungEntity>();
                DmGhiChuCachDungEntity obj = null;
                string sql = "spDmGhiChuCachDung_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmGhiChuCachDungEntity();
                        obj.MaGhiChuCachDung = reader["MaGhiChuCachDung"].ToString();
                        obj.TenGhiChuCachDung = reader["TenGhiChuCachDung"].ToString();
                        obj.MoTaGhiChu = reader["MoTaGhiChu"].ToString();
                        obj.TrangThaiSuDung = (bool)reader["TrangThaiSuDung"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmGhiChuCachDungList", ex.Message); throw ex; }
        }

    }
}